﻿namespace Stopwatch.ViewModels
{
    using System.ComponentModel;

    public class TimeModel : INotifyPropertyChanged
    {
        public TimeModel()
            : this(Time.Zero)
        {
        }

        public TimeModel(Time time)
        {
            Update(time);
        }

        public void Update(Time time)
        {
            Hours = time.Hours;
            Minutes = time.Minutes;
            Seconds = time.Seconds;
            Subseconds = time.Subseconds;
        }

        private int hours;
        public int Hours
        {
            get
            {
                return hours;
            }
            set
            {
                if (value != hours)
                {
                    hours = value;
                    NotifyPropertyChanged("Hours");
                }
            }
        }

        private int minutes;
        public int Minutes
        {
            get
            {
                return minutes;
            }
            set
            {
                if (value != minutes)
                {
                    minutes = value;
                    NotifyPropertyChanged("Minutes");
                }
            }
        }

        private int seconds;
        public int Seconds
        {
            get
            {
                return seconds;
            }
            set
            {
                if (value != seconds)
                {
                    seconds = value;
                    NotifyPropertyChanged("Seconds");
                }
            }
        }

        private int subseconds;
        public int Subseconds
        {
            get
            {
                return subseconds;
            }
            set
            {
                if (value != subseconds)
                {
                    subseconds = value;
                    NotifyPropertyChanged("Subseconds");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
